package com.wanmao.test;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanmao.test.bean.XSJLBean;
import com.wanmao.test.framgent.Framgent_Test_Search;
import com.wanmao.test.model.InstitutionModel;
import com.wanmao.test.service.ToolShowDemo;
import com.wanmao.test.service.XSJLService;
import com.wanmao.test.util.DateUtils;
import com.wanmao.test.util.StaticUtil;
import com.wanmao.test.view.XListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class PatrolRecordActivity extends BaseActivity {
    private XListView lv_device;
    private MyAdapter adapter;
    private String RWID;
    private XSJLService xsjlService = new XSJLService(this);
    private List<XSJLBean> xsjlBeanList;
    private EditText et_search;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_record);
        Intent intent =getIntent();
        RWID=intent.getStringExtra("RWID");
        initData();
        intenView();

    }

    private void initData() {
        xsjlBeanList = new ArrayList<>();
        xsjlBeanList = xsjlService.findByRWID(RWID);

    }

    private void intenView() {
        adapter= new MyAdapter(PatrolRecordActivity.this,xsjlBeanList);
        lv_device=(XListView) findViewById(R.id.lv_patrol_recird);
        lv_device.setAdapter(adapter);
        lv_device.setPullLoadEnable(false);
        lv_device.setPullRefreshEnable(false);

        et_search= (EditText) findViewById(R.id.et_XSJL);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                List<XSJLBean> result;
                if (TextUtils.isEmpty(s)) {
                    result = xsjlBeanList;
                } else {
                    result = new ArrayList<>();
                    for (XSJLBean m : xsjlBeanList) {
                        if (m.getXSRYMC().contains(s.toString())||m.getBDZMC().contains(s.toString()))
                            result.add(m);
                    }
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    TransitionManager.beginDelayedTransition(lv_device);
                }
                lv_device.setAdapter(new MyAdapter(PatrolRecordActivity.this,result));
            }
        });
    }
    class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Context mContext;
        private List<XSJLBean> mDatas;

        public MyAdapter(Context context, List<XSJLBean> mDatas) {
            mInflater = LayoutInflater.from(context);
            this.mContext = context;
            this.mDatas = mDatas;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_pator_record, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_item_pator_title);
                viewHolder.btn_do = (Button) convertView.findViewById(R.id.btn_item_pator_record);
                viewHolder.jl_id =(TextView) convertView.findViewById(R.id.JL_ID);
                viewHolder.xsry =(TextView) convertView.findViewById(R.id.xsry);
                viewHolder.ksTime =(TextView) convertView.findViewById(R.id.ksTime);
                viewHolder.jsTime =(TextView) convertView.findViewById(R.id.jsTime);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder)convertView.getTag();
            }
            String BDZ = mDatas.get(position).getBDZMC();
            final String JLID = mDatas.get(position).getOBJ_ID();
            String KSSL = mDatas.get(position).getXSKSSJ();
            String JSSJ = mDatas.get(position).getXSJSSJ();
            String XSRY = mDatas.get(position).getXSRYMC();
            viewHolder.tv_title.setText(BDZ);
            viewHolder.jl_id.setText("记录ID:"+JLID);
            viewHolder.ksTime.setText(KSSL);
            viewHolder.jsTime.setText(JSSJ);
            viewHolder.xsry.setText(XSRY);
            viewHolder.btn_do.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(PatrolRecordActivity.this, PatrolDetailsActivity.class);
                    intent.putExtra("XSJLID", JLID);
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }


    public static class ViewHolder {
        public TextView tv_title;
        public Button btn_do;
        public TextView jl_id;
        public TextView xsry;
        public TextView ksTime;
        public TextView jsTime;
    }




}
